package org.brownflat.watergate.domain;

import org.brownflat.utils.util.SecretEncoder;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>&copy Rupert Jones 2011</p>
 *
 * @author rup
 */
@Entity
@Table(name = "wgate_user")
public class User extends BaseEntity {

    @Column(name="username")
    private String username;

    @Column(name = "full_name")
    private String fullName;

    @Column
    private String password;

    @LazyCollection(LazyCollectionOption.FALSE)
    @OneToMany
    @JoinTable(
            name = "wgate_user_role",
            joinColumns = @JoinColumn(name = "user_id"),
            inverseJoinColumns = @JoinColumn(name = "role_id")
    )
    private Set<Role> roles = new HashSet<Role>();

    @LazyCollection(LazyCollectionOption.FALSE)
    @OneToMany
    @JoinTable(
            name = "wgate_user_group",
            joinColumns = @JoinColumn(name = "user_id"),
            inverseJoinColumns = @JoinColumn(name = "group_id")
    )
    private Set<Group> groups = new HashSet<Group>();

    @LazyCollection(LazyCollectionOption.FALSE)
    @OneToMany(mappedBy="user", orphanRemoval = true)
    @Access(value= AccessType.PROPERTY)
    @Cascade( { org.hibernate.annotations.CascadeType.ALL } )
    private List<UserRepository> userRepositories = new ArrayList<UserRepository>();

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public List<UserRepository> getUserRepositories() {
        return userRepositories;
    }

    public void setUserRepositories(List<UserRepository> userRepositories) {
        this.userRepositories = userRepositories;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setCleartextPassword(String password) {
        this.password = SecretEncoder.hash(password);
    }

    public Set<Group> getGroups() {
        return groups;
    }

    public void setGroups(Set<Group> groups) {
        this.groups = groups;
    }

    public void addGroup(Group group) {
        this.groups.add(group);
    }

    public void addGroups(List<Group> groups) {
        this.groups.addAll(groups);
    }

    public Set<Role> getRoles() {
        return roles;
    }

    public void setRoles(Set<Role> roles) {
        this.roles = roles;
    }

    public void addRole(Role role) {
        this.roles.add(role);
    }

    public void addUserRepository(UserRepository userRepository) {
        this.userRepositories.add(userRepository);
    }

    public List<String> getAliasFor(Repository repository) {
        List<String> aliases  = new ArrayList<String>();
        for(UserRepository userRepository : userRepositories) {
            if(repository.equals(userRepository.getRepository())) {
                aliases.add(userRepository.getAlias());
            }
        }
        return aliases;
    }

    public boolean isInGroup(Group group) {
        return groups.contains(group);
    }
}
